package com.xgk.boot.module.core.controller.admin.channel;


import com.xgk.boot.framework.common.core.KeyValue;
import com.xgk.boot.framework.common.enums.CommonStatusEnum;
import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageReqVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageRespVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiRespVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiSaveReqVO;
import com.xgk.boot.module.core.dal.entity.channel.ApiServiceDO;
import com.xgk.boot.module.core.dal.entity.dict.DictDataDO;
import com.xgk.boot.module.core.dal.iservice.ServiceProviderAPIService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 服务商管理")
@RestController
@RequestMapping("/admin/spapi")
@Validated
public class ServiceProviderApiController {

    @Resource
    private ServiceProviderAPIService spService;

    @PostMapping("/create")
    @Operation(summary = "创建API")
    public CommonResult<Long> createApi(@Valid @RequestBody ApiSaveReqVO createReqVO) {
        return success(spService.createApi(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "修改API")
    public CommonResult<Boolean> updateApi(@Valid @RequestBody ApiSaveReqVO updateReqVO) {
        spService.updateApi(updateReqVO);
        return success(true);
    }

    @PostMapping("/active")
    @Operation(summary = "修改API")
    public CommonResult<Boolean> activeApi(@RequestParam Long asId, @RequestParam Integer asStatus) {
        spService.activeApi(asId, asStatus);
        return success(true);
    }


    @GetMapping("/page")
    @Operation(summary = "获得API分页")
    public CommonResult<PageResult<ApiPageRespVO>> getApiPage(ApiPageReqVO pageReqVO) {
        PageResult<ApiServiceDO> pageResult = spService.getApiPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApiPageRespVO.class));
    }

    @Operation(summary = "/查询API列表")
    @GetMapping(value = "/selectList")
    public CommonResult<List<KeyValue>> getApiSelectList() {
        List<ApiServiceDO> list = spService.getApiSelectList();
        List<KeyValue> maps = list.stream().map(d -> new KeyValue<>(d.getAsId(), d.getAsName() + "[" + d.getAsCode() + "]")).collect(Collectors.toList());
        return success(maps);
    }
}
